package com.xiaoxu.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoxu.spzx.manager.mapper.ProductDetailsMapper;
import com.xiaoxu.spzx.manager.mapper.ProductMapper;
import com.xiaoxu.spzx.manager.mapper.ProductSkuMapper;
import com.xiaoxu.spzx.manager.service.ProductService;
import com.xiaoxu.spzx.model.dto.product.ProductDto;
import com.xiaoxu.spzx.model.entity.product.Product;
import com.xiaoxu.spzx.model.entity.product.ProductDetails;
import com.xiaoxu.spzx.model.entity.product.ProductSku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {


    @Autowired
    private ProductMapper productMapper;
    //向productSKu 添加信息
    @Autowired
    private ProductSkuMapper productSkuMapper;
    //向 productDetails 添加信息
    @Autowired
    private ProductDetailsMapper productDetailsMapper;


    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {

        PageHelper.startPage(page,limit);
        List<Product> list =  productMapper.findByPage(productDto);
        return new PageInfo<>(list);
    }

    @Override
    public void save(Product product) {
        //设置一些东西
        product.setStatus(0);              // 设置上架状态为0
        product.setAuditStatus(0);         // 设置审核状态为0
        //1.保存商品信息

        productMapper.save(product);

        //2.保存商品sku信息
        List<ProductSku> productSkuList = product.getProductSkuList();
        //遍历每一个productSkuList
        for (int i = 0; i < productSkuList.size(); i++) {
            //得到每一个对象
            ProductSku productSku = productSkuList.get(i);

            //初始化一些信息
            productSku.setSkuCode(product.getId() + "_" + i);       // 构建skuCode

            productSku.setProductId(product.getId());               // 设置商品id
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            productSku.setSaleNum(0);                               // 设置销量
            productSku.setStatus(0);
            productSkuMapper.save(productSku); // 保存数据
        }
        //3.保存商品详情表
        // 保存商品详情数据
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);

    }

    @Override
    public Product getById(Long id) {

        //1.得到product表中的信息
         Product  product = productMapper.findProductById(id);


         //2.得到product——sku表的信息
         List<ProductSku> productSkuList =   productSkuMapper.findSkuById(id);

         product.setProductSkuList(productSkuList);


         //3.得到product_details信息
        ProductDetails productDetails =  productDetailsMapper.selectByProductId(product.getId());
        product.setDetailsImageUrls(productDetails.getImageUrls());


        return product;
    }

    //实现修改信息的
    @Override
    public void updateById(Product product) {
        // 修改商品基本数据
        productMapper.updateById(product);

        // 修改商品的sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSkuMapper.updateById(productSku);
        });

        // 修改商品的详情数据
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        product.setDetailsImageUrls(productDetails.getImageUrls());
    }

    @Override
    public void deleteById(Long id) {
        productMapper.deleteById(id);                   // 根据id删除商品基本数据
        productSkuMapper.deleteByProductId(id);         // 根据商品id删除商品的sku数据
        productDetailsMapper.deleteByProductId(id);     // 根据商品的id删除商品的详情数据
    }
    //审核
    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {

        Product product = new Product();
        product.setId(id);
        if(auditStatus == 1) {
            product.setAuditStatus(1);
            product.setAuditMessage("审批通过");
        } else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        productMapper.updateById(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if(status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);
    }


}
